<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="120px"
        :rules="formRules"
      >
        <el-form-item label="姓名" prop="username" >
          <el-input v-model="formData.username" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone" >
          <el-input v-model="formData.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="省份" prop="province" >
          <el-input v-model="formData.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="市" prop="city" >
          <el-input v-model="formData.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区/县" prop="area" >
          <el-input v-model="formData.area" placeholder="请输入区/县" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入详细地址"/>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import { addressEdit } from '@/api/consumer'
import Popup from "@/components/popup/index.vue";
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑收货地址" : "新增收货地址";
});
const formData = reactive({
  username: "",
  phone: "",
  province:'',
  city:'',
  area:'',
  address:''
});

const formRules = {
  username: [
    {
      required: true,
      message: "请输入姓名",
      trigger: "blur",
    },
  ],
  phone: [
    {
      required: true,
      message: "请输入手机号",
      trigger: "blur",
    },
  ],
  province: [
    {
      required: true,
      message: "请输入省份",
      trigger: "blur",
    },
  ],
  city: [
    {
      required: true,
      message: "请输入市",
      trigger: "blur",
    },
  ],
  area: [
    {
      required: true,
      message: "请输入区/县",
      trigger: "blur",
    },
  ],
  address: [
    {
      required: true,
      message: "请输入详细地址",
      trigger: "blur",
    },
  ],
};

const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

const setFormData = (data: Record<any, any>) => {
  for (const key in formData) {
    if (data.info[key] != null && data.info[key] != undefined) {
      //@ts-ignore
      formData[key] = data.info[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  for (let formDataKey in row) {
    if (formData.hasOwnProperty(formDataKey)) {
      formData[formDataKey] = row[formDataKey]
    }
  }
};

const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
  getDetail,
});
//提交
const handleSubmit = async () => {
	// formData.rank_id = formData.id
  await formRef.value?.validate();
  await addressEdit(formData);
  popupRef.value?.close();
  emit("success");
};
</script>
